package com.topscore.uke.ui.activities;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.os.UserManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;

import com.topscore.uke.R;
import com.topscore.uke.entities.UserInfo;
import com.topscore.uke.main.ATManager;
import com.topscore.uke.ui.BaseCompatActivity;
import com.topscore.uke.utils.UserInfoManager;

public class SplashActivity extends BaseCompatActivity {

    private Handler mHandler;

    private final int _Home_Page = 1023, _Login_Page = 1024, _Navigation_Page = 2048;

    private final long _DelayedTimeMillis = 2500;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ATManager.add(this);
        setContentView(R.layout.activity_splash);
        startToIntent();
    }

    private void startToIntent() {
        mHandler = new Handler(msg -> {
            switch (msg.what) {
                case _Home_Page:
                    startActivity(new Intent(mContext, MainCompatActivity.class));
                    finish();
                    break;
                case _Login_Page:
                    startActivity(new Intent(mContext, LoginActivity.class));
                    finish();
                    break;
            }
            return false;
        }
        );
        mHandler.sendEmptyMessageDelayed(UserInfoManager.getUserInfo() == null ? _Login_Page : _Home_Page, _DelayedTimeMillis);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ATManager.backExit(mContext);
        }
        return false;
    }
}
